package biz.datalk.industrialland.admin.modules.online.dynform.service.impl;

import biz.datalk.commons.utils.IdProvider;
import biz.datalk.industrialland.admin.modules.online.dynform.mapper.IDynformButtonExtMapper;
import biz.datalk.industrialland.admin.modules.online.dynform.mapper.IDynformButtonMapper;
import biz.datalk.industrialland.admin.modules.online.dynform.mapper.IDynformHeadMapper;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.dto.DynformButtonDTO;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformButton;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformButtonExample;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformHead;
import biz.datalk.industrialland.admin.modules.online.dynform.service.IDynformButtonService;
import biz.datalk.industrialland.common.def.enums.DBYNEnum;
import biz.datalk.industrialland.common.exception.ServiceException;
import biz.datalk.industrialland.common.result.UnifyResult;
import biz.datalk.industrialland.common.util.I18NUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author tarofang@163.com
 * @date 2020-01-02
 *
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class DynformButtonServiceImpl implements IDynformButtonService {

    private final IDynformButtonMapper dynformButtonMapper;
    private final IDynformButtonExtMapper dynformButtonExtMapper;

    @Autowired
    public DynformButtonServiceImpl(IDynformButtonMapper dynformButtonMapper, IDynformButtonExtMapper dynformButtonExtMapper) {
        this.dynformButtonMapper = dynformButtonMapper;
        this.dynformButtonExtMapper = dynformButtonExtMapper;
    }

    private IDynformHeadMapper dynformHeadMapper;

    @Autowired
    public void setDynformHeadMapper(IDynformHeadMapper dynformHeadMapper) {
        this.dynformHeadMapper = dynformHeadMapper;
    }

    @Override
    public UnifyResult list(DynformButtonDTO dynformButtonDTO) {
        checkTableIdExists(dynformButtonDTO);
        DynformButtonExample example = new DynformButtonExample();
        example.setOrderByClause("btn_order ASC");

        DynformButtonExample.Criteria criteria = example.createCriteria();
        criteria.andTableIdEqualTo(dynformButtonDTO.getTableId());

        PageHelper.startPage(dynformButtonDTO.getPageNumber(), dynformButtonDTO.getPageSize());
        List<DynformButton> list = dynformButtonMapper.selectByExample(example);
        PageInfo<DynformButton> pageInfo = new PageInfo<>(list);
        return UnifyResult.success().mapData().put("rows", pageInfo.getList()).put("total", pageInfo.getTotal());
    }

    @Override
    public UnifyResult add(DynformButtonDTO dynformButtonDTO) {

        checkTableIdExists(dynformButtonDTO);

        dynformButtonDTO.setId(IdProvider.createUUIDId());
        dynformButtonDTO.setIsDelete(DBYNEnum.N.name());

        dynformButtonMapper.insertSelective(dynformButtonDTO);

        return UnifyResult.success();
    }

    @Override
    public UnifyResult edit(DynformButtonDTO dynformButtonDTO) {
        checkTableIdExists(dynformButtonDTO);

        String id = dynformButtonDTO.getId();
        DynformButton existsBtn = dynformButtonMapper.selectByPrimaryKey(id);
        if (existsBtn == null) {
            return UnifyResult.fail(I18NUtil.getMsg("validations.online.dynform.button.no.exists.or.delete"));
        }

        dynformButtonMapper.updateByPrimaryKeySelective(dynformButtonDTO);

        return UnifyResult.success();
    }

    @Override
    public UnifyResult delete(DynformButtonDTO dynformButtonDTO) {
        String id = dynformButtonDTO.getId();
        DynformButton existsBtn = dynformButtonMapper.selectByPrimaryKey(id);
        if (existsBtn == null) {
            return UnifyResult.fail(I18NUtil.getMsg("validations.online.dynform.button.no.exists.or.delete"));
        }

        dynformButtonMapper.deleteByPrimaryKey(id);

        return UnifyResult.success();
    }

    @Override
    public UnifyResult detail(DynformButtonDTO dynformButtonDTO) {
        String id = dynformButtonDTO.getId();
        DynformButton existsBtn = dynformButtonMapper.selectByPrimaryKey(id);
        if (existsBtn == null) {
            return UnifyResult.fail(I18NUtil.getMsg("validations.online.dynform.button.no.exists.or.delete"));
        }

        return UnifyResult.success().setData(existsBtn);
    }

    @Override
    public List<DynformButton> getButtons(String tableId, String btnStyle) {
        if (StringUtils.isBlank(tableId)) {
            return new ArrayList<>();
        }
        DynformHead dynformHead = dynformHeadMapper.selectByPrimaryKey(tableId.trim());
        if (dynformHead == null) {
            return new ArrayList<>();
        }

        List<DynformButton> btns = dynformButtonExtMapper.selectByTableIdAndBtnStyle(dynformHead.getId(), btnStyle);
        btns = CollectionUtils.isEmpty(btns) ? new ArrayList<>() : btns;

        return btns;
    }


    private void checkTableIdExists(DynformButtonDTO dynformButtonDTO) {
        if (StringUtils.isBlank(dynformButtonDTO.getTableId())) {
            throw new ServiceException(I18NUtil.getMsg("validations.online.dynform.button.invalid.blank.tableId"));
        }

        DynformHead exists = dynformHeadMapper.selectByPrimaryKey(dynformButtonDTO.getTableId());
        if (exists == null) {
            throw new ServiceException(I18NUtil.getMsg("dynform.head.no.exists.or.delete"));
        }
    }
}
